using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using ECommerce.Library.Web;
using System.Data;

namespace ECommerce.Library.Data
{
    public abstract class BaseDAO
    {
        protected List<BaseDAO> GetListData(DbCommand command)
        {
            try
            {
                if (command.Connection.State != ConnectionState.Open)
                    command.Connection.Open();

                DbDataReader reader = command.ExecuteReader();
                List<BaseDAO> entity = BindData(reader);
                return entity;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                command.Connection.Close();
                command.Dispose();
            }
        }

        protected object GetOneData(DbCommand command)
        {
            try
            {     
                if (command.Connection.State != ConnectionState.Open)
                    command.Connection.Open();

                object data = command.ExecuteScalar();
                object result = command.Parameters["@ReturnValue"].Value;

                if (data == null && result != null)
                    return result;
                else if (result == null && data != null)
                    return data;
                else
                    return data;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                command.Connection.Close();
                command.Dispose();
            }
        }

        protected void SetCommandType(DbCommand command, CommandType cmdType, string cmdText)
        {
            if (string.IsNullOrEmpty(cmdText))
                throw (new ArgumentNullException("cmdText in BaseDAO.SetCommandType()"));

            if (command == null)
                throw (new ArgumentNullException("sqlCmd in BaseDAO.SetCommandType()"));

            command.CommandType = cmdType;
            command.CommandText = cmdText;
        }

        protected string FormatXMLForIdArray(System.Collections.Generic.List<Int32> idsToList)
        {
            //converting the list to xml first
            StringBuilder xmlString = new StringBuilder();
            for (int i = 0; i < idsToList.Count; i++)
            {
                xmlString.AppendFormat("<Id>{0}</Id>", idsToList[i]);
            }

            return xmlString.ToString();
        }

        protected string FormatXMLForIdArray(System.Collections.Generic.List<String> idsToList)
        {
            //converting the list to xml first
            StringBuilder xmlString = new StringBuilder();
            for (int i = 0; i < idsToList.Count; i++)
            {
                xmlString.AppendFormat("<Id>{0}</Id>", idsToList[i]);
            }

            return xmlString.ToString();
        }

        protected abstract DbCommand CreateCommand();
        public abstract bool Save();
        public abstract bool Create();
        public abstract bool Delete();
        public abstract bool Load();
        protected abstract List<BaseDAO> BindData(DbDataReader reader);  
    }
}
